import random
import requests
from retry import retry
from loguru import logger
import my_fake_useragent as ua1
from Api.Proxy import Proxy_IP

ua = ua1.UserAgent(family="chrome")
requests.packages.urllib3.disable_warnings()

# waiwang_proxies_list = [
#     {'https': 'http://192.168.66.51:808', 'http': 'http://192.168.66.51:808'},
#     {'https': 'http://192.168.66.37:808', 'http': 'http://192.168.66.37:808'},
#     {'https': 'http://192.168.66.118:808', 'http': 'http://192.168.66.118:808'},
#     {'https': 'http://192.168.66.130:808', 'http': 'http://192.168.66.130:808'},
#     {'https': 'http://192.168.66.139:808', 'http': 'http://192.168.66.139:808'},
# ]

waiwang_proxies_list = [
	{'https': 'http://192.168.44.149:7890', 'http': 'http://192.168.44.149:7890'},
	{'https': 'http://192.168.44.148:7890', 'http': 'http://192.168.44.148:7890'},
	{'https': 'http://192.168.44.147:7890', 'http': 'http://192.168.44.147:7890'},
	{'https': 'http://192.168.44.157:7890', 'http': 'http://192.168.44.157:7890'},
	{'https': 'https://YingKe:%qq123456..@ftp.new66.net:521', 'http': 'https://YingKe:%qq123456..@ftp.new66.net:521'},
]



class Request2:
    def __init__(self):
        self.headers = {
            "user-agent": str(ua.random())
        }

    @retry(tries=5, delay=1)
    def GetResponse(self, url, S=None, method="GET", is_proxy=None, params=None, data=None, json=None, headers=None,
                    timeout: int = 20, verify=False, allow_redirects=True, cookies=None,
                    error_code=[400, 403, 404, 502, 500], return_S=False,stream=None):
        try:
            if S == None:
                S = requests.Session()
                if headers == None:
                    S.headers = {
                        "user-agent": str(ua.random())
                    }
                else:
                    S.headers = headers
            if not is_proxy:
                S.proxies = Proxy_IP.get_proxy()
                # S.proxies = None
                # pass
            elif is_proxy == 'waiwang':
                S.proxies = random.choice(waiwang_proxies_list)
                # logger.info(f'使用外网代理：{S.proxies}')
            elif is_proxy == 'no':
                # S.proxies = None
                pass
            else:
                S.proxies = is_proxy
            if method == "GET":
                response = S.get(url, timeout=timeout, params=params, allow_redirects=allow_redirects, verify=verify,cookies=cookies,stream=stream)
            elif method == "POST":
                response = S.post(url, params=params, timeout=timeout, data=data, json=json, allow_redirects=allow_redirects,verify=verify, cookies=cookies,stream=stream)
            else:
                if return_S:
                    return f"暂未支持该模式：{method},请联系作者处理！", ''
                return f"暂未支持该模式：{method},请联系作者处理！"
            if response.status_code in error_code:
                logger.error(f'状态码在{error_code}：{response.status_code}，{url},{response.url},{response.text[:100]}')
                raise ValueError(f'状态码在{error_code}')
            if return_S:
                return response, S
            return response
        except Exception as e:
            # logger.warning(e)
            raise


Request2 = Request2()